package com.example.stray_animal_management_system.admin.controller;

import com.example.stray_animal_management_system.entity.User;
import com.example.stray_animal_management_system.service.UserService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/2
 */
@RestController
@RequestMapping("/api/admin/user")
public class UserAdminController {

    private final UserService userService;

    public UserAdminController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/list")
    public Map<String, Object> list() {
        List<User> users = userService.list();
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "查询成功");
        res.put("data", users);
        return res;
    }

    /**
     * 根据ID查询用户详情
     */
    @GetMapping("/{id}")
    public Map<String, Object> getUserById(@PathVariable Integer id) {
        User user = userService.getById(id);
        Map<String, Object> res = new HashMap<>();
        if (user == null) {
            res.put("code", 404);
            res.put("msg", "用户不存在");
            return res;
        }
        res.put("code", 200);
        res.put("data", user);
        return res;
    }

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public Map<String, Object> addUser(@RequestBody User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        userService.save(user);
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "用户添加成功");
        return res;
    }

    /**
     * 更新用户
     */
    @PutMapping("/update")
    public Map<String, Object> updateUser(@RequestBody User user) {
        user.setUpdateTime(LocalDateTime.now());
        userService.updateById(user);
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "用户信息已更新");
        return res;
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteUser(@PathVariable Integer id) {
        userService.removeById(id);
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("msg", "用户已删除");
        return res;
    }

    /**
     * 修改领养配额
     */
    @PutMapping("/quota")
    public Map<String, Object> updateQuota(@RequestParam("id") Integer id,
                                           @RequestParam("quota") Integer quota) {
        User user = userService.getById(id);
        Map<String, Object> res = new HashMap<>();
        if (user == null) {
            res.put("code", 404);
            res.put("msg", "用户不存在");
            return res;
        }
        user.setAdoptionQuota(quota);
        userService.updateById(user);
        res.put("code", 200);
        res.put("msg", "配额修改成功");
        return res;
    }
    /**
     * 管理员重置用户密码
     */
    @PutMapping("/resetPassword")
    public Map<String, Object> resetPassword(@RequestParam("id") Integer id) {
        Map<String, Object> res = new HashMap<>();
        User user = userService.getById(id);

        if (user == null) {
            res.put("code", 404);
            res.put("msg", "用户不存在");
            return res;
        }

        // 默认重置密码为 "123456"
        String defaultPwd = "123456";

        // ✅ 推荐加密存储（使用 Spring Security 提供的加密类）
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPwd = encoder.encode(defaultPwd);

        user.setPassword(encodedPwd);
        userService.updateById(user);

        res.put("code", 200);
        res.put("msg", "密码已重置为默认值（123456）");
        return res;
    }

}